package com.mobapping;


import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.mobapping.IDataAdapter.ElementInfo;
import com.mobapping.IDataAdapter.IArrayListDataListener;

public class Harita extends MapActivity {
	/** Called when the activity is first created. */

	private final int GPS_AKTIF_HALE_GETIRILMELI=0;
	private LocationManager konumYoneticisi;
	private LocationListener konumDinleyicisi;
	private MapView haritaGorunumu;
	private IDataAdapter  webAdapter;
	Drawable kameramarker;
	Drawable greenmarker;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		greenmarker = this.getResources().getDrawable(R.drawable.greenmarkerr);
		kameramarker = this.getResources().getDrawable(R.drawable.kamera);
		
		initialize_konumdinleyicisi();

		super.onCreate(savedInstanceState);
		setContentView(R.layout.harita);
		haritaGorunumu = (MapView) findViewById(R.id.mapview);
		haritaGorunumu.setBuiltInZoomControls(true);
		GeoPoint nokta = new GeoPoint((int)(38 * 1e6), (int) (36 * 1e6));
		haritaGorunumu.getController().setCenter(nokta);
		konumYoneticisi= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		//webAdapter = new DataAdapterSim();
		webAdapter = new DataAdapter(this);
			final KameraItemizedOverlay itemizedoverlay_kameralar = new KameraItemizedOverlay(kameramarker,haritaGorunumu.getContext());
			double city_latitude=39.92;	
			double city_longitude=32.82;	

			final GeoPoint city_center= new GeoPoint((int)(city_latitude* 1e6) , (int)(city_longitude* 1e6));

			webAdapter.getCameras( new IArrayListDataListener() { 

				public void dataArrived(ArrayList<Object> data) {
					List <Overlay> mapOverLays= haritaGorunumu.getOverlays();
					mapOverLays.clear();
					haritaGorunumu.getController().setCenter(city_center);
					haritaGorunumu.getController().setZoom(13);
					if (data != null) {
						int size = data.size();
						if (size > 0) {
							final GeoPoint kameralocations[]= new GeoPoint[size];
							final ElementInfo[] kameralar = new ElementInfo[size];
							for (int i = 0; i < size; i++) {
								kameralar[i]= (ElementInfo)data.get(i); 
								kameralocations[i]=new GeoPoint((int)(kameralar[i].lattitude* 1e6),(int)(kameralar[i].longitude* 1e6));
								KameraOverlayItem overlayitem = new KameraOverlayItem(kameralocations[i], kameralar[i].name,  kameralar[i].id);
								itemizedoverlay_kameralar.addOverlay(overlayitem);
							}
							mapOverLays.add(itemizedoverlay_kameralar);
						}
					}
				}
			});
			if(konumYoneticisi.isProviderEnabled(LocationManager.GPS_PROVIDER)==true)
			{
				Thread locationUpdates = new Thread(){
					public void run(){
						Looper.prepare();
						Criteria criteria = new Criteria();
						criteria.setAccuracy(Criteria.ACCURACY_FINE);

						String provider = konumYoneticisi.getBestProvider(criteria, true);

						konumYoneticisi.requestLocationUpdates(provider, 0, 1.0f, konumDinleyicisi);
						konumYoneticisi.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, konumDinleyicisi);
						Looper.loop();							
					}
				};
				locationUpdates.start();
			}
			else {
				Toast.makeText(getApplicationContext(), getString(R.string.gps_uyari), Toast.LENGTH_LONG).show();
			}

	
	}


	



	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		MenuInflater inflater = getMenuInflater();

		inflater.inflate(R.menu.harita_menu, menu);
		return true;

	}

	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case R.id.konumumu_haritada_goster:
			konumYoneticisi= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
			if(konumYoneticisi.isProviderEnabled(LocationManager.GPS_PROVIDER)==true)
			{
				Thread locationUpdates = new Thread(){
					public void run(){
						Looper.prepare();
						konumYoneticisi.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, konumDinleyicisi);
						Looper.loop();							
					}
				};
				locationUpdates.start();
			}
			else {
				showDialog(GPS_AKTIF_HALE_GETIRILMELI);
			}
			break;

		}
		return super.onOptionsItemSelected(item);
	}


	@Override
	protected Dialog onCreateDialog(int id){
		switch(id)
		{
		case GPS_AKTIF_HALE_GETIRILMELI:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(R.string.gps_aktif_hale_getiriniz).setNeutralButton(R.string.tamam, new OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which ){
					dialog.cancel();
				}
			});
			return builder.create();
		}
		return super.onCreateDialog(id);

	}


	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}


	private void initialize_konumdinleyicisi(){
		konumDinleyicisi = new LocationListener() {
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras){

			}

			@Override
			public void onProviderEnabled(String provider){

			}

			@Override
			public void onProviderDisabled(String provider){


			}
			@Override
			public void onLocationChanged(Location location){
				final KameraItemizedOverlay itemizedoverlay_konumum = new KameraItemizedOverlay(greenmarker,haritaGorunumu.getContext());
				final KameraItemizedOverlay itemizedoverlay_kameralar = new KameraItemizedOverlay(kameramarker,haritaGorunumu.getContext());
				konumYoneticisi.removeUpdates(konumDinleyicisi);
				if(location != null)
				{
					final GeoPoint nokta= new GeoPoint((int)(location.getLatitude()* 1e6) , (int)(location.getLongitude()* 1e6));
					//    			OverlayItem overlayitem = new OverlayItem(nokta, "Konumum", "");
					KameraOverlayItem overlayitem = new KameraOverlayItem(nokta, "Konumum",0);

					itemizedoverlay_konumum.addOverlay(overlayitem);

					webAdapter.getCameras(new IArrayListDataListener() { 

						public void dataArrived(ArrayList<Object> data) {
							List <Overlay> mapOverLays= haritaGorunumu.getOverlays();
							mapOverLays.clear();
							mapOverLays.add(itemizedoverlay_konumum);
							haritaGorunumu.getController().setCenter(nokta);
							haritaGorunumu.getController().setZoom(13);
							if (data != null) {
								int size = data.size();
								if (size > 0) {
									final GeoPoint kameralocations[]= new GeoPoint[size];
									final ElementInfo[] kameralar = new ElementInfo[size];
									for (int i = 0; i < size; i++) {
										kameralar[i]= (ElementInfo)data.get(i); 
										kameralocations[i]=new GeoPoint((int)(kameralar[i].lattitude* 1e6),(int)(kameralar[i].longitude* 1e6));
										KameraOverlayItem overlayitem = new KameraOverlayItem(kameralocations[i], kameralar[i].name,  kameralar[i].id );
										itemizedoverlay_kameralar.addOverlay(overlayitem);
									}
									mapOverLays.add(itemizedoverlay_kameralar);
								}
							}							
						}
					});




				}
				else{
					Toast.makeText(getApplicationContext(), R.string.gps_konum_bilgisi_hesaplanamadi,Toast.LENGTH_LONG).show();

				}

			}
		};


	}


}